home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 January / enter-2004-01.iso / files / maxima-5.9.0.exe / {app} / share / maxima / 5.9.0 / demo / macex.dem < prev    next >
Encoding:
Text File  |  2003-02-09  |  2.8 KB  |  116 lines

  1. /* ==================================================================== */
  2. /* file: macex.dem     */
  3. /* At some point, we get the message: DISPLACE undefined */
  4.  
  5.  
  6. /* This is a demo of Macsyma's MACROEXPANDing commands and the use of
  7. the MACROEXPANSION switch.  */
  8.  
  9. (SHOWTIME:ALL,MACROEXPANSION:FALSE)$
  10.  
  11. /* First we need a macro to play with.  Let's define a CASEQ statement
  12. of the form:
  13.  
  14. CASEQ(<var>,[<keys1>],<stmt1>,
  15.             [<keys2>],<stmt2>,
  16.         ....,
  17.         [<keysn>],<stmtn>)
  18.  
  19. where the first <stmt> that has <var> as a member of the associated
  20. <keys> is the one chosen to execute.  */
  21.  
  22. CASEQ(VAR,[PAIRS])::=
  23.      IF LENGTH(PAIRS)<=2
  24.         THEN BUILDQ([VAR,KEYS:FIRST(PAIRS),STATEMENT:LAST(PAIRS)],
  25.             IF MEMBER(VAR,'KEYS) THEN STATEMENT)
  26.         ELSE BUILDQ([VAR,KEYS:FIRST(PAIRS),
  27.                      STATEMENT:FIRST(REST(PAIRS)),
  28.                    PAIRS:REST(REST(PAIRS))],
  29.             IF MEMBER(VAR,'KEYS) 
  30.                THEN STATEMENT
  31.                ELSE CASEQ(VAR,SPLICE(PAIRS)))$
  32.  
  33. /* Let's use our CASEQ macro to define a simple predicate.  */
  34.  
  35. VARIABLEP(X)::=BUILDQ([X],CASEQ(X,[A,B,C,D,E],TRUE));
  36.  
  37. /* To make sure it works.  */
  38.  
  39. DISPLAY(VARIABLEP(VAR1),VARIABLEP(VAR2)),VAR1:'A,VAR2:'Z;
  40.  
  41. /* We can see what VARIABLEP is expanding into.  */
  42.  
  43. MACROEXPAND(VARIABLEP(SOME_FORM));
  44.  
  45. /* We can also watch the expansion by stages.  */
  46.  
  47. MACROEXPAND1(VARIABLEP(SOME_FORM));
  48.  
  49. MACROEXPAND1(''%);
  50.  
  51. /* We might also create simple TYPEP macro using CASEQ.  */
  52.  
  53.  
  54. TYPEP(X)::=BUILDQ([X],CASEQ(X,[1,2,3,4,5,6,7,8,9,0], 'DIGIT,
  55.                   [A,B,C,D,E,F,G,H,I,J], 'VARIABLE,
  56.                   ["+","-","*","/","^"], 'OPERATOR))$
  57.  
  58. /* Let's see what things are expanding into.  */
  59.  
  60. MACROEXPAND(TYPEP(TEST));
  61.  
  62. /* The nested CASEQ doesn't expand because MACROEXPAND and
  63. MACROEXPAND1 only look at the top level function.  To expand all
  64. levels we can do:  */
  65.  
  66. SCANMAP('MACROEXPAND,'(TYPEP(TEST)));
  67.  
  68. /* Let's test it just to make sure it works.  */
  69.  
  70. EV(%,TEST="*");
  71.  
  72. /* Compare the time it just took to evaluate the macro when expanded
  73. to the time it takes to expand it from scratch and then eval.  */
  74.  
  75. EV(TYPEP(TEST),TEST="*");
  76.  
  77. /* This is why we can save time by using the MACROEXPANSION switch.  */
  78.  
  79. FORM:'(TYPEP(A));
  80.  
  81. EV(FORM);
  82.  
  83. MACROEXPANSION:EXPAND;
  84.  
  85. EV(FORM);
  86.  
  87. /* There's no savings on the first call after MACROEXPANSION has been
  88. reset, but there is on all subsequent calls.  */
  89.  
  90. EV(FORM);
  91.  
  92. /* MACROEXPAND also uses the saved expansion. */
  93.  
  94. MACROEXPAND(''FORM);
  95.  
  96. /* Note that FORM still displays nicely.  */
  97.  
  98. FORM;
  99.  
  100. /* If we set MACROEXPANSION to DISPLACE however, each macro call will
  101. be completely replaced by the equivalent code.  */
  102.  
  103. /* (MACROEXPANSION:DISPLACE,EV(FORM));
  104.  
  105. FORM;
  106. */
  107. /*  Note that once the call is displaced, the original call cannot be
  108. retrieved by resetting MACROEXPANSION.  */
  109.  
  110. /* (MACROEXPANSION:FALSE,EV(FORM));
  111.  
  112. FORM;
  113. */ 
  114. SHOWTIME:FALSE$
  115.  
  116.